function checkCompany(){
	
	var labels = document.getElementById("compId");
	var comp = document.getElementById("companyId").value;
 

	if (comp.length == 0) {
		document.getElementById("companyId").style.border = "2px solid  red";
		labels.innerHTML = "<font color='red'>公司代码不能为空</font>";
		return false;
	} else {
		$.ajax({
			type : "GET",
			url : "/HFG/comp/check",
			data : {
				companyId : $("#companyId").val()
			},
			dataType : "json",
			success : function(data) {
				if (data.result == "error") {
					document.getElementById("companyId").style.border = "2px solid  red";
					labels.innerHTML = "<font color='red'>"	+ data.message + "</font>";
					return false;
				} 
//				else if (data.result == "errors") {
//					document.getElementById("companyId").style.border = "2px solid  red";
//					labels.innerHTML = "<font color='red'>"+ data.message + "</font>";
//					document.getElementById("companyId").value = "";
//					return false;
//				} 
				else {
					labels.innerHTML = "<font color='green'>"+ data.message + "</font>";
					document.getElementById("companyId").style.border = "2px solid  green";
					return true;
				}
			}
		});
	}
};

function checkFile() {
	var labels = document.getElementById("files");
	var file = document.getElementById("fileField").value;
	var companyId = document.getElementById("companyId").value;
	 if (typeof String.prototype.startsWith != 'function') { 
		 String.prototype.startsWith = function (str){
         return this.slice(0, str.length) == str;
      };
    }
　　　
    // 判断当前字符串是否以str结束
    if (typeof String.prototype.endsWith != 'function') {
      String.prototype.endsWith = function (str){
         return this.slice(-str.length) == str;
      };
    }
    // 测试程序
    var sBeCompareStr = ".txt";// 被比较字符串

	if (file.length == 0) {
		document.getElementById("fileField").style.border = "2px solid red";
		labels.innerHTML = "<font color='red'>凭证文件不能为空</font>";
		return false;
	} else {
		var names = file.substring(0, file.lastIndexOf("."));
		var sunms = names.substring(names.lastIndexOf("_") + 1);
		if(file.indexOf("SSC_" + companyId.toUpperCase() + "_") < 0){
			alert(companyId.toUpperCase());
 			//document.getElementById("fileField").style.border = "2px solid red";
 			//labels.innerHTML = "<font color='red'>凭证文件格式不合法,不是SSC_" + companyId.toUpperCase() + "_开头的文件</font>";
 			return false;
		}  else if( file.endsWith(sBeCompareStr)==false){
 			document.getElementById("fileField").style.border = "2px solid red";
 			labels.innerHTML = "<font color='red'>凭证文件格式不合法,不是.txt文件</font>";
 			return false;
		} else if(isNaN(sunms) == true){
			document.getElementById("fileField").style.border = "2px solid red";
 			labels.innerHTML = "<font color='red'>凭证文件格式不合法,"+sunms+"不是正整数</font>";
 			return false;
		} else if(sunms.length > 14|| sunms.length < 14){
			document.getElementById("fileField").style.border = "2px solid red";
			labels.innerHTML = "<font color='red'>凭证文件格式不合法,"+sunms+"长度不够14位</font>";
			return false;
		}else{
		    var pos=file.lastIndexOf("\\");
			labels.innerHTML = "<font color='green'>"+ file.substring(pos+1) + "</font>";
			document.getElementById("fileField").style.border = "2px solid green";
			return true;
		}
	}
};
function upload(){
	 if(checkCompany()==false||checkFile()==false){
		 return false;
	 }
	var labels = document.getElementById("mess");
	var formData = new FormData($("#queryForm")[0]);
	$.ajax({
		url : '/HFG/record/uploadFile',
		type : 'POST',
		data : formData,
		dataType : "json",
		//async : false,
		contentType : false,
		processData : false,
		success : function(data) {
			if (data.result == "error") {
				_refresh();
				alert(data.message);
			} else {
				labels.innerHTML = "<font color='green'>"+ data.message + "</font>";
				_refresh();
				clean_out();
			}	
		}
	});
};

function _refresh(){
	   $.post(
			   "record/getRunArray",
		         function (msg){
		             $("#impos").html(msg);
		        }
			);
};


function clean_out(){
 
	document.getElementById("companyId").value="";
	document.getElementById("companyId").style.border = "";
	document.getElementById("compId").innerHTML = "";
	
	document.getElementById("fileField").value="";
	document.getElementById("fileField").style.border = "";
	document.getElementById("files").innerHTML = "";
 
	$("#compId").empty();    
	$("#files").empty();   
    
};

function checksReset(){
	var labels = document.getElementById("compId");
	document.getElementById("companyId").value="";
	document.getElementById("companyId").style.border = "";
	labels.innerHTML = "";
	
	document.getElementById("fileField").value="";

	document.getElementById("fileField").style.border = "";
	$("#compId").empty();    
	$("#files").empty();   
	$("#mess").empty();   
};
 